package map;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by 佳星 on 08-09-0009.
 */
public class CoordMap extends TouchImage implements PointExtension{
    private Map<Object, MapPoint> points = new HashMap<Object, MapPoint>();
    private OnPointActionListener onPointActionListener;
    public CoordMap(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CoordMap(Context context) {
        super(context);
    }

    public void setOnPointActionListener(OnPointActionListener onPointActionListener) {
        this.onPointActionListener = onPointActionListener;
    }

    @Override
    public void addPoint(MapPoint point) {
        points.put(point.tag, point);
        postInvalidate();
    }

    @Override
    public void removePoint(Object tag) {
        if (points.containsKey(tag)){
            points.remove(tag);
            postInvalidate();
        }
    }

    @Override
    public void clearPoint() {
        points.clear();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (MapPoint point : points.values()){
            point.onDraw(canvas);
        }
        canvas.save();
    }

    @Override
    protected void onViewClick(float xOnView, float yOnView) {
        Log.v("d","onclick");

        if (onPointActionListener == null){
            return;
        }
        for (MapPoint point : points.values()){
            if ( point.inArea(xOnView, yOnView)){
                onPointActionListener.onPointClick(point, xOnView, yOnView);
                break;//同时只有一个点可以被点击
            }
        }
    }




    @Override
    protected void postScale(float scaleFactor, float scaleCenterX, float scaleCenterY) {
        super.postScale(scaleFactor, scaleCenterX, scaleCenterY);
        if (scaleFactor != 0){
            for (MapPoint point : points.values() ){
                point.onScale(scaleFactor, scaleCenterX, scaleCenterY);
            }
        }
    }

    @Override
    void postTranslate(float distX, float distY) {
        super.postTranslate(distX, distY);
        if ( !(distX == 0 && distY == 0) ){
            for (MapPoint point : points.values()){
                point.onTranslate(distX, distY);
            }
        }
    }

    public List<MapPoint> getPoints() {
        return new ArrayList<MapPoint>(points.values());
    }
}
